package com.kye.express.business.help.list;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.express.databinding.FragmentHelpBinding;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.deposit.ResultDepositDetailedBean;
import com.kye.model.bean.help.RequestGetHelpList;
import com.kye.model.bean.help.ResultGetHelpListBean;
import com.kye.model.datasource.HelpDataSource;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 帮助 Fragment的Presenter
 * <p>
 * Created by SkyYu on 2018/01/9
 */
public interface HelpFragmentContract {

    interface HelpFragmentView extends IView {

        FragmentHelpBinding getBinding();

        HelpItemTypeModel getType();

        void refreshInitSuccess(List<ResultGetHelpListBean> result);

        void refreshInitApiError(OpenApiResult<List<ResultGetHelpListBean>> result, int code, String msg);

        void refreshInitOtherError(Throwable e);

        void refreshSuccess(List<ResultGetHelpListBean> result);

        void refreshApiError(OpenApiResult<List<ResultGetHelpListBean>> result, int code, String msg);

        void refreshOtherError(Throwable e);
    }

    class HelpFragmentPresenter extends BasePresenter<HelpFragmentView> {

        HelpDataSource helpDataSource;

        HelpFragmentPresenter() {
            helpDataSource = new HelpDataSource();
        }

        /**
         * 第一次加载网络请求（获取帮助列表信息接口）
         */
        void refreshInit(int classId) {
            helpDataSource.getHelpDataList(new RequestGetHelpList(classId, 100))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<List<ResultGetHelpListBean>>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<ResultGetHelpListBean>> result, List<ResultGetHelpListBean> helpInfoListModelList) {
                            getView().refreshInitSuccess(result.getData());
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<ResultGetHelpListBean>> result, int code, String msg) {
                            getView().refreshInitApiError(result,code,msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().refreshInitOtherError(e);
                        }
                    });
        }

        /**
         * 下拉刷新网络请求（获取帮助列表信息接口）
         */
        void refresh(int classId) {
            helpDataSource.getHelpDataList(new RequestGetHelpList(classId, 100))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<List<ResultGetHelpListBean>>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<ResultGetHelpListBean>> result, List<ResultGetHelpListBean> helpInfoListModelList) {
                            getView().refreshSuccess(result.getData());
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<ResultGetHelpListBean>> result, int code, String msg) {
                            getView().refreshApiError(result,code,msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().refreshOtherError(e);
                        }
                    });
        }
    }
}